package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.ShopGuide;
import pers.cj.pdos.entity.Store;
import pers.cj.pdos.service.IShopGuideService;
import pers.cj.pdos.service.IStoreService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.ShopGuideVo;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/user")
public class ShopGuideController extends BaseController {
    @Autowired
    private IShopGuideService shopGuideService;

    @RequiresPermissions("set:set")
    @RequestMapping("/shopGuide")
    public String shopGuide(){
        return "user/shopGuide";
    }

    @ResponseBody
    @RequiresPermissions("set:set")
    @RequestMapping("/shopGuide/getShopGuides")
    public ResponseList getShopGuides(int page, int limit, String searchStoreId, String searchContent) {
        Company company = getCompany();
        List<ShopGuideVo> list = shopGuideService.listVoPage(page,limit,company.getId(),searchStoreId,null,searchContent);

        //返回数据
        return ResponseList.success(shopGuideService.count(company.getId(),null), list);
    }

    @ResponseBody
    @RequiresPermissions("set:set")
    @RequestMapping("/shopGuide/add")
    public ResponseEntity add(@RequestBody ShopGuide shopGuide){
        Company company = getCompany();
        if (shopGuideService.checkName(shopGuide.getName(),company.getId())){
            return ResponseEntity.fail("导购员名称已存在");
        }
        DataUtil.processEntity(company,shopGuide);
        shopGuideService.insert(shopGuide);

        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("set:set")
    @RequestMapping("/shopGuide/delete")
    public ResponseEntity delete(@RequestBody ShopGuide shopGuide){
        try {
            shopGuideService.remove(shopGuide);
            return ResponseEntity.success();
        } catch (Exception e) {
            return ResponseEntity.fail("已被使用无法删除");
        }
    }

    @ResponseBody
    @RequiresPermissions("set:set")
    @RequestMapping("/shopGuide/update")
    public ResponseData update(@RequestBody ShopGuide shopGuide, boolean checkName){
        if (checkName && shopGuideService.checkName(shopGuide.getName(),getCompany().getId())){
            return ResponseData.fail("名称已存在");
        }
        DataUtil.processEntity(false, getCompany(), shopGuide);
        shopGuideService.update(shopGuide);

        return ResponseData.success(shopGuideService.getVoById(shopGuide.getId()));
    }
}
